//
//  ClientVC.swift
//  BonjourBothSidesOniOS
//
//  Created by LevyGG on 2019/8/23.
//  Copyright © 2019 LevyGG. All rights reserved.
//

import UIKit

class ClientVC: UIViewController {

    private var bonjourClient: BonjourClient!
    

    @IBOutlet weak var toSendTextField: UITextField!
    @IBOutlet weak var fromServerTextField: UITextField!
    @IBOutlet weak var sentButton: UIButton!
    @IBOutlet weak var connectedToLabel: UILabel!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Client"
        self.hideKeyboardWhenTappedAround()
        
        bonjourClient = BonjourClient()
        bonjourClient.delegate = self
        
    }
    
    @IBAction func sendToServer(_ sender: UIButton) {
        if let data = toSendTextField.text!.data(using: String.Encoding.utf8) {
            bonjourClient.send(data)
        }
    }
    
}

extension ClientVC:BonjourClientDelegate {
    func connectedTo(_ socket: GCDAsyncSocket!) {
        connectedToLabel.text = "Connected to " + socket.connectedHost
    }
    
    func disconnected() {
        connectedToLabel.text = "Disconnected"
    }
    
    func handleBody(_ body: NSString?) {
        fromServerTextField.text = body as String?
    }

    func handleHeader(_ header: UInt) {
        
    }
}
